package com.cljz.peipei.present.activity;

import android.app.Activity;

import com.cljz.peipei.base.App;
import com.cljz.peipei.base.BaseActivity;
import com.cljz.peipei.base.BaseP;
import com.cljz.peipei.model.net.api.Api;
import com.cljz.peipei.model.net.bean.EmojiWordBean;
import com.cljz.peipei.model.net.bean.GetUserInfoBean;
import com.cljz.peipei.model.net.bean.ResponseBean;
import com.cljz.peipei.model.net.bean.TagListBean;
import com.cljz.peipei.ui.activity.MainActivity;
import com.cljz.peipei.utils.T;
import com.clzj.peipei.bean.Tag;
import com.clzj.peipei.dao.TagDao;

import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.router.Router;

/**
 * Created by apple on 2017/4/19.
 */

public class MainP extends BaseP<MainActivity> {

    public void emojiWord() {
        Api.getResponseInfoAPI().emojiWordData(getToken())
                .compose(XApi.<EmojiWordBean>getApiTransformer())
                .compose(XApi.<EmojiWordBean>getScheduler())
                .compose(getV().<EmojiWordBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<EmojiWordBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }
                    @Override
                    public void onNext(EmojiWordBean emojiWordbean) {
                        if ("0".equals(emojiWordbean.getCode())) {
                            getV().getEmojiWordSucceed(emojiWordbean);
                        } else if ("1000".equals(emojiWordbean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getEmojiWordFail(emojiWordbean.getMsg());
                        }
                    }
                });

    }

    public void exit(String token){
        Api.getResponseInfoAPI().logout(token)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {

                    }
                });
    }

    public List<Tag> getTagListForCache(){
        TagDao tagDao = daoSession.getTagDao();
        return  tagDao.loadAll();
    }

    public void getTagList() {
        Api.getResponseInfoAPI().tagListData(getToken(), getUserInfo(getToken()).getUser_id())
                .compose(XApi.<TagListBean>getApiTransformer())
                .compose(XApi.<TagListBean>getScheduler())
                .compose(getV().<TagListBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<TagListBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(TagListBean tagListBean) {
                        if ("0".equals(tagListBean.getCode())) {
                            //将myTag添加到数据库
                            List<TagListBean.DataBean.tagBean> myTag = tagListBean.getData().getMy();
                            TagDao tagDao = daoSession.getTagDao();
                            tagDao.deleteAll();
                            Tag tag;
                            for (TagListBean.DataBean.tagBean bean : myTag) {
                                tag = new Tag();
                                tag.setId(bean.getId());
                                tag.setTag_name(bean.getTag_name());
                                tagDao.insert(tag);
                            }
                            getV().getTagListSucceed(tagListBean, tagDao.loadAll());
                        } else if ("1000".equals(tagListBean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getTagListFail(tagListBean.getMsg());
                        }
                    }
                });
    }


    public void tagSet(String tagId, final Tag tag) {
        Api.getResponseInfoAPI().tagSetData(getToken(), tagId)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getCode())) {
                            getV().getTagSetSucceed(responseBean);
                            daoSession.getTagDao().insert(tag);
                        } else if ("1000".equals(responseBean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getTagSetFail(responseBean.getMsg());
                        }
                    }
                });
    }

    public void tagDel(final String tagId, final Tag bean) {
        Api.getResponseInfoAPI().tagDelData(getToken(), tagId)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getCode())) {
                            getV().getTagDelSucceed(responseBean);
                            daoSession.getTagDao().delete(bean);
                        } else if ("1000".equals(responseBean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getTagDelFail(responseBean.getMsg());
                        }
                    }
                });
    }

    public void getUserInfoData(String otherUserId) {
        Api.getResponseInfoAPI().getUserInfoData(getToken(), otherUserId)
                .compose(XApi.<GetUserInfoBean>getApiTransformer())
                .compose(XApi.<GetUserInfoBean>getScheduler())
                .compose(getV().<GetUserInfoBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<GetUserInfoBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(GetUserInfoBean getUserInfoBean) {
                        if ("0".equals(getUserInfoBean.getCode())) {
                            getV().getUserInfoSucceed(getUserInfoBean);
                        } else if ("1000".equals(getUserInfoBean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getUserInfoFail(getUserInfoBean.getMsg());
                        }
                    }
                });
    }


    public void callWait(int matchingSex, String myTags, int constellationIndex) {
        Api.getResponseInfoAPI().callWaitData(getToken(),String.valueOf(matchingSex),myTags,String.valueOf(constellationIndex))
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().callWaitFail(error.toString());
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getCode())) {
                            getV().callWaitSucceed(responseBean);
                        } else if ("1000".equals(responseBean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().callWaitFail(responseBean.getMsg());
                        }
                    }
                });
    }

    public void callEnd() {
        Api.getResponseInfoAPI().callEndData(getToken())
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getCode())) {
                            getV().callEndSucceed(responseBean);
                        } else if ("1000".equals(responseBean.getCode())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().callEndFail(responseBean.getMsg());
                        }
                    }
                });
    }
}
